﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaseMethods
{
    public static class Tools
    {
        public static double Mse(this int[,] arr)
        {
            var dif = new int[arr.Length / 2];
            for (int i = 0; i < arr.Length / 2; i++)
            {
                dif[i] = arr[i, 0] - arr[i, 1];
            }
            return dif.Sum(i => (i * i)) / ((double)arr.Length / 2);

        }
        public static double MseLabels(this int[,] arr, int nClass, double[] aposteriori)
        {
            var dif = new double[arr.Length / 2];
            for (int i = 0; i < arr.Length / 2; i++) // dla wszystkich obserwacji
            {
                for (int j = 1; j < nClass + 1; j++)// suma po labelach
                {
                    dif[i] += GetValue(nClass, i, arr[i, 1])*aposteriori[i];
                }

            }
            return dif.Average();
        }

        private static int GetValue(int nClass, int label, int value)
        {
            if (!Dict.ContainsKey(nClass))
            {
                Dict.Add(nClass, new Dictionary<int, Dictionary<int, int>>());
            }
            var dictLabels = Dict[nClass];
            if (!dictLabels.ContainsKey(label))
            {
                dictLabels.Add(label, new Dictionary<int, int>());
            }
            var dictVal = dictLabels[label];

            if (!dictVal.ContainsKey(value))
            {
                dictVal.Add(value, (int)Math.Pow(value - label, 2));
            }
            return dictVal[value];
        }

        // Numer of labels , label number , value 
        public static Dictionary<int, Dictionary<int, Dictionary<int, int>>> Dict = new Dictionary<int, Dictionary<int, Dictionary<int, int>>>();
    }
}
